package cn.edu.njuit.springbootdemo.controller;

import cn.edu.njuit.springbootdemo.javabean.Article;
import cn.edu.njuit.springbootdemo.javabean.dto.ArticleDTO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

//@Controller
@RestController
@RequestMapping("/v1")
public class ArticleController {

    @GetMapping("/articles/{id}")
    public Article getArticle(@PathVariable(name = "id") Long articleId) {
        //模拟从数据库中读取了相应编号的文章
        Article article = new Article();
        article.setId(articleId);
        article.setTitle("习近平会见几内亚比绍总统");

//        String jsonString = JSON.toJSONString(article);
        return article;
    }

    @GetMapping("/articles")
    public List<Article> getArticles() {
        //假设从数据读取全部的文章
        List<Article> articles = new ArrayList<>();
        articles.add(new Article(1L, "暑期文旅 新供给带来新体验（经济聚焦）"));
        articles.add(new Article(2L, "【数说经济】深化改革推动新旧动能接续转换"));
        articles.add(new Article(1L, "漫评美将多家中国实体列入出口管制清单：\"再加一个\""));
        return articles;
    }


    @PutMapping("/articles")
    public String updateArticle(@Validated @RequestBody ArticleDTO article) {
        System.out.println("id=" + article.getId() + " title=" + article.getTitle());


        return "修改成功";
    }

}
